/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Vue;

import Modele.Album;
import Modele.Bibliotheque;
import Modele.ModeleImage;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;


/**
 *
 * @author Soufiane GARCH
 */
public class VueAlbum extends JPanel{

    private Bibliotheque biblio;
    private Vector albums;
    private Album albumActuel;
    private DefaultListModel dlm;
    private JList list;
    private JPopupMenu menu;
    private JLabel label;
    private VuePrincipale vuePrincipale;
    
    
    public VueAlbum(Bibliotheque biblio, VuePrincipale vP){
        this.vuePrincipale = vP;
        label = new JLabel("Album");
        this.biblio = biblio;
        this.albums = this.biblio.getAlbum();
        this.dlm = new DefaultListModel();
        this.albumActuel = null;
        creerList();
        this.add(list);
        this.setLayout(null);
        setPropertiesForList();
        setPropoerties();
        
        list.addMouseListener(new MouseListener() 
		{
			public void mouseClicked(MouseEvent e)
			{
                            vuePrincipale.getBiblio().setRepertoireActuel(null);
                            albumActuel=(Album) list.getSelectedValue();
                            vuePrincipale.afficheMiniature(albumActuel);
                            vuePrincipale.setImageActuel(null);
								
			}
			public void mouseEntered(MouseEvent e) {}
			public void mouseExited(MouseEvent e) {}
			public void mousePressed(MouseEvent e) {}
			public void mouseReleased(MouseEvent e) {}
		});
		
		
		addMouseListener(new MouseListener() 
		{
			public void mouseClicked(MouseEvent e)
			{
				if(albumActuel!=null)
				{
                                    vuePrincipale.getBiblio().setRepertoireActuel(null);
                                    albumActuel=(Album) list.getSelectedValue();
                                    vuePrincipale.afficheMiniature(albumActuel);
                                    vuePrincipale.setImageActuel(null);
				}
				
			}
			public void mouseEntered(MouseEvent e) {}
			public void mouseExited(MouseEvent e) {}
			public void mousePressed(MouseEvent e) {}
			public void mouseReleased(MouseEvent e) {}
		});
                DropTarget dropTarget = new DropTarget(this.list, new DragAndDrop(this.list));
    }
    
    public void ajouterAlbum(Album album){
        dlm.addElement(album);
    }
    
    public void deleteAlbum(Album album){
        if (dlm.contains(album)){
            dlm.removeElement(album);
        }
    }
    
    private void setPropertiesForList(){
        //list.setVisibleRowCount(10);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }
    
    private void setPropoerties(){
        this.setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
		
        JPanel lab = new JPanel();
	lab.add(label);
	this.add(lab);
		
		
	JScrollPane scrollPane= new JScrollPane(list);
	scrollPane.setPreferredSize(new Dimension(280, 300));
	this.add(scrollPane);
    }
    /*
    private void setPopupMenu(){
        JMenuItem Ajouter = new JMenuItem("Ajouter");
        //JMenuItem Renommer = new JMenuItem("Renommer");
        JMenuItem Supprimer = new JMenuItem("Supprimer");

        
        menu = new JPopupMenu();
        menu.add(Ajouter);
        //menu.add(Renommer);
        menu.add(Supprimer);
    }
    */
    private void creerList(){
        this.list = new JList(dlm);
        if (albums != null){
            Iterator it = albums.iterator();
            while(it.hasNext()){
                ajouterAlbum( (Album) it.next());
            }
        }
    }
            
    public Album getAlbumActuel(){
        return this.albumActuel;
    }
    
    public void update(){
        Iterator it = albums.iterator();
        while(it.hasNext()){
            Album tmpAlbum = (Album) it.next();
            
            if (!(dlm.contains(tmpAlbum))){
                ajouterAlbum(tmpAlbum);
            }
        }
        
        for(int i = 0; i < dlm.getSize(); i++){
            Album tmpAlbum = (Album) dlm.get(i);
            if (!albums.contains(tmpAlbum)){
                deleteAlbum(tmpAlbum);
            }
        }
    }
           
    public void setAlbumActuel(){
        albumActuel = (Album) list.getSelectedValue();
    }
    
    public void setAlbumActuel(Album al){
        albumActuel = al;
    }

    public JList getList(){
        return list;
    }
    
    class DragAndDrop implements DropTargetListener {
    
    private JList list;
    
    public DragAndDrop(JList list){
        
        this.list = list;
    }
    
    
    public void dragEnter(DropTargetDragEvent dtde) {}
    
    public void dropActionChanged(DropTargetDragEvent dtde) {}
    
    public void dragExit(DropTargetEvent dte) {}
    
    
    public void dragOver(DropTargetDragEvent e) {
        
        //set cursor location. Needed in setCursor method
        Point cursorLocationBis = e.getLocation();
        
        
        if(this.list.contains(e.getLocation())){
            e.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE ) ;
        } else{
            
            e.rejectDrag();
        }
        
    }
    
    
    public void drop(DropTargetDropEvent e) {
        
        try {
            Transferable tr = e.getTransferable();
            
            if (!tr.isDataFlavorSupported( VueImageMiniature.FLAVOR)) e.rejectDrop();
            
            String chemin = (String) tr.getTransferData(VueImageMiniature.FLAVOR );
            
            if(!list.contains(e.getLocation())) {
                    e.rejectDrop();
                    return;
            }
            
            int index = this.list.locationToIndex(e.getLocation());
            
            if (index != -1){
                this.list.setSelectedIndex(index);
                Album al = (Album) this.list.getSelectedValue();
                if (al == null) return;
                ModeleImage im = new ModeleImage(chemin);
                al.ajouterImage(im);
                list.repaint();
            }
            
            /*
            Element pere=null;
            if(index!=-1){
                this.list.setSelectedIndex(index);
                Element photo=(Element)this.list.getSelectedValue();
                if(photo instanceof Dossier) pere=photo;
                else pere= ((AdaptateurModelListeVirtuelle)list.getModel()).getModel();
                
                
            }else{
                pere= ((AdaptateurModelListeVirtuelle)list.getModel()).getModel();
            }
            
            
            
            
      
            
            
            File f = new File(pathAbsolu);
            Element fils = new Photo(new String(f.getName()),pere,f);
            
            pere.addElement(fils);
            */
            int action = e.getDropAction();
            boolean copyAction = (action == DnDConstants.ACTION_COPY);
            
            
            try {
                if (!copyAction) ;
                
                if (copyAction) e.acceptDrop(DnDConstants.ACTION_COPY);
                else e.acceptDrop(DnDConstants.ACTION_MOVE);
            } catch (java.lang.IllegalStateException ils) {
                e.rejectDrop();
            }
            
            e.getDropTargetContext().dropComplete(true);
            
        }catch(IOException io){
            e.rejectDrop(); 
        }catch(UnsupportedFlavorException ufe){
            e.rejectDrop();
        }
        
    }
    
}
    
}
